package com.bjc.nio;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @program: nettypro
 * @description:  将缓冲区写入到通道
 * @author: bjc
 * @create: 2021-09-03 20:43
 */
public class NIOFileChannel01 {

    public static void main(String[] args) throws Exception{
        String str = "Hello World";
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\iotest_delete\\file01.txt");
        //通过fileOutPutStream获取对应的FileChannel
        //这个fileChannel真是类型是FileChannelImpl
        FileChannel fileChannel = fileOutputStream.getChannel();
        //创建一个缓冲区ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //将str放入byteBuffer
        byteBuffer.put(str.getBytes());
        //对byteBuffer进行反转flip
        byteBuffer.flip();
        //将byteBuffer数据写入到fileChannel
        fileChannel.write(byteBuffer);
        fileOutputStream.close();
    }
}
